﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Common;
using System.Data.SqlClient;
using WeifenLuo.WinFormsUI.Docking;
using System.Xml;

using Entity.Core;

namespace CreateEntity
{
    public partial class DataBaseTree : WeifenLuo.WinFormsUI.Docking.DockContent
    {
        MainResource main;
        public DataBaseTree(MainResource main)
        {
            this.main = main;
            InitializeComponent();
        }

        private void DataBaseTree_Load(object sender, EventArgs e)
        {
            LoadDataBase();
        }

        /// <summary>
        /// 加载数据库
        /// </summary>
        public void LoadDataBase()
        {
            dbTree.Nodes.Clear();//先清除一下。

            Connections conn = new Connections();//得到所有连接字符串
            TreeNode server = new TreeNode("服务器", 0, 0);//服务器

            IList<ConnectionModel> connectionModes = conn.GetConnectionStrings();
            foreach (ConnectionModel model in connectionModes)
            {
                TreeNode node = new TreeNode(model.DataBaseName, 1, 1);
                node.Name = model.Provider;
                node.ToolTipText = model.ConnectionString;
                server.Nodes.Add(node);
            }
            dbTree.Nodes.Add(server);
            server.Expand();
            main.UpdateState("数据库加载完成");
        }

        //数据库树上右键菜单
        private void mItemCreate_Click(object sender, EventArgs e)
        {
            if (dbTree.SelectedNode.Parent != null)
            {
                EntityCode code = new EntityCode(main);
                code.Text = dbTree.SelectedNode.Text;
                GetTableColumnsByConnection column = new GetTableColumnsByConnection(dbTree.SelectedNode.Parent.ToolTipText, dbTree.SelectedNode.Parent.Name, dbTree.SelectedNode.Text);

                code.RichTextBox.Text = Entity.Core.CreateEntity.CreateCode(column.GetDataReader(), dbTree.SelectedNode.Text);
                code.Show(main.DockPanel, DockState.Document);
                main.UpdateState("生成完成");
            }
            else
            {
                MessageBox.Show("请选择表");
            }
        }

        //右击选中节点
        private void dbTree_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            this.dbTree.SelectedNode = e.Node;
        }

        //双击打开代码窗口
        private void dbTree_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Node.Parent != null)
            {
                if (e.Node.Parent.Text != "服务器")
                {
                    //防止双击数据库名，执行第一个节点的双击事件
                    if (e.Node.Text == dbTree.SelectedNode.Text)
                    {
                        EntityCode code = new EntityCode(main);
                        code.Text = dbTree.SelectedNode.Text;

                        GetTableColumnsByConnection column = new GetTableColumnsByConnection(e.Node.Parent.ToolTipText, e.Node.Parent.Name, e.Node.Text);

                        code.RichTextBox.Text = Entity.Core.CreateEntity.CreateCode(column.GetDataReader(), e.Node.Text);
                        code.Show(main.DockPanel, DockState.Document);
                        main.UpdateState("生成完成");
                    }
                }
                else//双击的是数据库名
                {
                    if (e.Node.Nodes.Count == 0)//防止加载
                    {
                        LoadTables(e.Node);
                    }
                }
            }
        }

        /// <summary>
        /// 加载表
        /// </summary>
        /// <param name="e">要加载节点</param>
        private void LoadTables(TreeNode currentNode)
        {
            ConnectionModel mode = new ConnectionModel() {ConnectionString=currentNode.ToolTipText,DataBaseName=currentNode.Text,Provider=currentNode.Name};
            Connections connection = new Connections();
            if (connection.CheckConnection(mode))
            {
                GetTablesByConnection tables = new GetTablesByConnection(mode);
                DataTable dbTable = tables.GetTables();
                foreach (DataRow row in dbTable.Rows)
                {
                    TreeNode node = new TreeNode(row["table_name"].ToString(), 2, 2);
                    node.ContextMenuStrip = contextMenuStrip1;
                    currentNode.Nodes.Add(node);
                }
                currentNode.Expand();
                main.UpdateState("数据表加载完成");
            }
            else
            {
                MessageBox.Show("连接数据库失败！", "示", MessageBoxButtons.OK, MessageBoxIcon.Question);
            }
        }

        private void tbtnSart_Click(object sender, EventArgs e)
        {
            if (dbTree.SelectedNode.Parent != null)
            {
                if (dbTree.SelectedNode.Parent.Text == "服务器")
                {
                    LoadTables(dbTree.SelectedNode);
                }
                else
                {
                    main.UpdateState("请选择数据库");
                }
            }
        }

        private void tbtnDel_Click(object sender, EventArgs e)
        {
            if (dbTree.SelectedNode.Parent != null)
            {
                if (dbTree.SelectedNode.Parent.Text == "服务器")
                {
                    ConnectionModel model = new ConnectionModel();
                    model.ConnectionString = dbTree.SelectedNode.ToolTipText;
                    model.DataBaseName = dbTree.SelectedNode.Text;
                    model.Provider = dbTree.SelectedNode.Name;
                    Connections conn = new Connections();
                    if (conn.Delete(model))
                    {
                        main.UpdateState("删除完成");
                        LoadDataBase();
                    }
                    else
                    {
                        main.UpdateState("删除失败");
                    }
                }
                else
                {
                    main.UpdateState("请选择数据库");
                }
            }
        }

        private void tbtnRefresh_Click(object sender, EventArgs e)
        {
            LoadDataBase();
        }

        private void tbtnAdd_Click(object sender, EventArgs e)
        {
            DataBaseType type = new DataBaseType(this);
            type.ShowDialog();
        }
    }
}
